using System;
using CookComputing.XmlRpc;
using Drupalxmlrpc.DrupalNode;

namespace Drupalxmlrpc
{
    public class DrupalNodeApiServer
    {
        private IDrupalNodeApi proxy = null;
        private DrupalSystem Conf;

    public DrupalNodeApiServer()
    {
        proxy = XmlRpcProxyGen.Create<IDrupalNodeApi>();
    }

        /**
         * Create new drupal node and return nid
         */
        public string Add() //DrupalNode node
        {
            DrupalNodeInterval node = new DrupalNodeInterval("Test New Features Again", "Some text 777");

            //---------------------------------------------------------------

           //field_hash_md5
            StringField stringField = new StringField("field_hash_md5");
            stringField.Add("Fail!");
            node.field_hash_md5 = stringField.GetData();

            //---------------------------------------------------------------

//            node.field_length = new IntField[1];
//            node.field_length[0].name = "field_length";
//            node.field_length[0].value = 12;


            if (node.uid == null)
            {
                node.uid = Convert.ToString(Conf.user.uid);
            }
            return proxy.NodeAdd(Conf.sessid, node);
        }

        /**
         * Connect to the xml_rpc server and save "sessid" and "user" in "data" structure
         */
        public DrupalSystem Connect()
        {
            DrupalSystem data = proxy.Connect();
            SaveSessionId(data);
            return data;
        }

        private void SaveSessionId(DrupalSystem drupal)
        {
            Conf = drupal;
        }

        public DrupalSystem Login(string username, string password)
        {
            DrupalSystem data = proxy.Login(Conf.sessid, username, password);
            SaveSessionId(data);
            return data;
        }

        public DrupalNodeInterval nodeInit(DrupalNodeInterval node)
        {
            node.type = "page";
            node.title = "Test!";
            node.body = "Some text";
            node.nid = "";
            return node;
        }
    }
}